package org.duoyi.striver.service.common;

import com.develop.jawin.COMException;
import lombok.extern.slf4j.Slf4j;
import org.duoyi.jdm.Dm.DmInstant;
import org.duoyi.striver.StriverApplication;
import org.duoyi.striver.consts.Area;
import org.duoyi.striver.consts.Picture;
import org.duoyi.striver.consts.Shortcuts;
import org.duoyi.striver.global.DmManager;
import org.duoyi.striver.model.impl.PointImpl;
import org.duoyi.striver.model.impl.Sleeper;

/**
 * 通用的服务功能
 */
@Slf4j
public class CommonService {

    /**
     * 是否战斗中，战斗中会等待战斗结束
     */
    public static boolean isFighting() {
        synchronized (StriverApplication.class) {
            Sleeper.sleep();
            if (! (Picture.战斗_取消自动战斗.find() || Picture.战斗_自动按钮.find())) {
                return false;
            }
            log.info("战斗中...");
            if (Picture.战斗_自动按钮.find()) {
                Area.战斗_自动战斗.leftClick();
            }
            Sleeper.sleep(5000);
            while (Picture.战斗_取消自动战斗.find()) {
                Sleeper.sleep(5000);
            }
            log.info("战斗结束");
            Sleeper.sleep(2000);
            isDefeated();
            return true;
        }
    }

    /**
     * 系统商店
     */
    public static boolean isSystemStore() {
        synchronized (StriverApplication.class) {

            if (!Picture.系统商店界面.find()) {
                return false;
            }
            log.info("系统商店购物");
            Area.系统商店购买按钮.leftClick();
            return true;
        }
    }

    /**
     * 逛摊
     */
    public static boolean isBoothStore() {
        synchronized (StriverApplication.class) {
            if (Picture.逛摊购物_未激活.find()) {
                Area.逛摊_激活.leftClick();
            } else if (!Picture.逛摊购物_已激活.find()) {
                return false;
            }
            log.info("逛摊购买");

            if (Picture.逛摊购物_摊位未激活.find()) {
                Area.逛摊_激活摊位.leftClick();
            }

            Area.逛摊_购买.leftClick();

            Sleeper.sleep();
            if (Picture.逛摊购物_摊位已激活.find()) {
                Area.逛摊_购买.leftClick();
            }

            return true;
        }
    }

    /**
     * 宠物商店
     */
    public static boolean isPetStore() {
        synchronized (StriverApplication.class) {
            if (!Picture.宠物商店_标识.find()) {
                return false;
            }
            log.info("宠物商店购买");
            Area.宠物商店_购买.leftClick();
            return true;
        }
    }

    /**
     * 打开日程面板
     */
    public static void openDailyPanel() {
        synchronized (StriverApplication.class) {
            //打开日程
            log.info("打开日程面板");
            while (!Picture.日程界面.find()) {
                Shortcuts.日程.press();
            }
        }
    }

    public static void closeAllPanel() {
        synchronized (StriverApplication.class) {
            if (!Picture.无界面_标识.find()) {
                log.info("关闭所有面板");
                Shortcuts.Esc.press();
            }
        }
    }

    public static boolean isDefeated() {
        synchronized (StriverApplication.class) {


            if (!Picture.战斗_失败标识.find()) {
                return false;
            }
            log.info("战斗失败，关闭失败面板");
            Area.战斗_关闭失败界面.leftClick();
            return true;
        }
    }

    public static boolean isNpcTaking() {
        synchronized (StriverApplication.class) {


            if (!Picture.NPC_对话框.find()) {
                return false;
            }
            log.info("NPC对话选择");
            Area.NPC_对话_第一项.leftClick();
            return true;
        }
    }

    public static boolean isTeamInviting() {
        synchronized (StriverApplication.class) {


            if (!Picture.邀请入队.find()) {
                return false;
            }
            log.info("拒绝入队邀请");
            Area.邀请入队_取消.leftClick();
            return true;
        }
    }

    public static boolean isGiftBox() {
        synchronized (StriverApplication.class) {


            if (!Picture.活跃礼盒.find()) {
                return false;
            }
            log.info("领取活跃礼盒");
            Area.活跃礼盒_领取.leftClick();
            return true;
        }
    }

    public static int bindWindows() throws COMException {
        DmInstant dmInstant = new DmInstant("dm.dmsoft");
        DmManager.put(dmInstant);
        int hwnd = dmInstant.FindWindow("", "神武4手游");
        int bind = dmInstant.BindWindow(hwnd, "dx2", "windows3", "windows", 0);
        if (bind != 1) {
            throw new RuntimeException("绑定游戏窗口失败");
        }
        return hwnd;
    }

    public static void release() {
        DmManager.clear();
        PointImpl.destroy();
    }

}
